/**
 * 
 */
package com.thoughtworks.merchantsguide.processor;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.merchantsguide.exception.InvalidInputException;
import com.thoughtworks.merchantsguide.exception.UnsupportedNumberException;

/**
 * This class reads the query and decides which exact Line processor should
 * process the query. This class acts as a bridge/mediator for the input to be
 * processed.
 *
 */
public class ProcessorDecider implements LineProcessor {

	private static final Logger LOGGER = Logger.getLogger(ProcessorDecider.class.getName());

	private static final String UNKNOWN_QUERY_RESPONSE = "I have no idea what you are talking about";

	/**
	 * @see com.thoughtworks.merchantsguide.processor.LineProcessor#process(java.lang.String)
	 */
	@Override
	public Object process(String query) {

		if (null != query) {
			if (StringUtils.split(query) != null && StringUtils.split(query).length == 3) {
				LOGGER.log(Level.INFO, query + " processed by GalacticNumberGrammarProcessor");
				try {
					new GalacticNumberGrammarProcessor().process(query);
				} catch (InvalidInputException e) {
					LOGGER.log(Level.SEVERE, e.getMessage());
				}
			} else if (StringUtils.endsWith(query, "Credits")) {
				LOGGER.log(Level.INFO, query + " processed by CommodityPriceProcessor");

				try {
					new CommodityPriceProcessor().process(query);
				} catch (UnsupportedNumberException e) {
					LOGGER.log(Level.SEVERE, e.getMessage());
				}
			} else if (StringUtils.startsWith(query, "how m")) {
				LOGGER.log(Level.INFO, query + " processed by CustomerQueryProcessor");
				try {
					String op = (String) new CustomerQueryProcessor().process(query);
					System.out.println(op);
				} catch (Exception e) {
					LOGGER.log(Level.SEVERE, e.getMessage());
					System.out.println(UNKNOWN_QUERY_RESPONSE);
				}
			}

		} else {
			throw new RuntimeException("Invalid Input.");
		}
		return Boolean.TRUE;
	}

}
